/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.administrator;

import assignment_cp3120.DAO.Factory.DAOFactory;
import assignment_cp3120.administrator.createmanager.RegisterManagerView;
import assignment_cp3120.administrator.deletemanager.DeleteManagerView;
import assignment_cp3120.administrator.editmanager.EditManagerModel;
import assignment_cp3120.administrator.editmanager.EditManagerView;
import assignment_cp3120.auth.Authenticate;
import assignment_cp3120.dataObjects.User;
import assignment_cp3120.startup.StartupView;
import assignment_cp3120.view.MainFrame;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class AdminView extends JPanel {    
    private static AdminView instance;
    
    private JButton createButton;
    private JPanel createPanel;
    private JLabel createLabel;
    
    private JButton editButton;
    private JPanel editPanel;
    private JLabel editLabel;
    
    private JButton deleteButton;
    private JPanel deletePanel;
    private JLabel deleteLabel;
    
    private JButton logoutButton;
    private JPanel logoutPanel;
    private JLabel logoutLabel;

    private JPanel previousContent;
    
    protected AdminView() {
        previousContent = StartupView.getInstance().getContents();
        createPanel();        
        StartupView.getInstance().setSubTitle("Welcome Admin: " + Authenticate.getInstance().getFirstname());        
        instance = this;
    }    
    private void createPanel(){        
        GridLayout layout = new GridLayout(0,1);
        FlowLayout fl = new FlowLayout();
        
        createButton = new JButton("Create");
        createPanel = new JPanel(fl);
        createLabel = new JLabel("Create a manager account");
        
        editButton = new JButton("Edit");
        editPanel = new JPanel(fl);
        editLabel = new JLabel("Edit a manager account");
        
        deleteLabel = new JLabel("Delete a manager account");
        deletePanel = new JPanel(fl);
        deleteButton = new JButton("Delete");
        
        logoutLabel = new JLabel("Logout of this account");
        logoutPanel = new JPanel(fl);
        logoutButton = new JButton("Logout");
        
        createPanel.add(createLabel);
        createPanel.add(createButton);
        
        editPanel.add(editLabel);
        editPanel.add(editButton);
        
        deletePanel.add(deleteLabel);
        deletePanel.add(deleteButton);
        
        logoutPanel.add(logoutLabel);
        logoutPanel.add(logoutButton);     
        
        add(createPanel);
        add(editPanel);
        add(deletePanel);
        add(logoutPanel);
        
        setLayout(layout);
        
        createButton.addActionListener(new CreateAL());
        editButton.addActionListener(new EditAL());
        deleteButton.addActionListener(new DeleteAL());
        logoutButton.addActionListener(new LogoutAL());
    }
    private class CreateAL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            new RegisterManagerView().showGUI();
        }        
    }
    private class EditAL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            EditManagerModel emModel = new EditManagerModel();
            String manager = (String)JOptionPane.showInputDialog(
                    null,
                    "Please enter the name of the manager you wish to edit",
                    "Manager Username",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    null,
                    null);
            
            if (DAOFactory.getDAOFactory().getUserDAO().getUser(manager) != null
                    && manager.length() > 0)
            {
                emModel.setManager(manager);
                new EditManagerView(emModel).showGUI();
            }
            else {
                JOptionPane.showMessageDialog(null, "Manager does not exist", "Mananger Not Found!", JOptionPane.ERROR_MESSAGE);
            }
        }        
    }
    private class DeleteAL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            DeleteManagerView.getInstance().showGUI();
        }        
    }    
    private class LogoutAL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            logout();
        }
    }
    /**
     * Function to logout, displays the welcome screen and logs user out
     */
    public void logout(){
        StartupView.getInstance().setSubTitle("");
        StartupView.getInstance().clearContents();
        StartupView.getInstance().setContents(previousContent);
        Authenticate.setInstance(new User());
    }
    /**
     * Get the current instance if its null create a new one
     * @return
     * instance
     */
    public static AdminView getInstance() {
        if (instance == null) {
            instance = new AdminView();
        }
        return instance;
        
    }
    
}
